package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.Post;
import org.example.system.entity.User;
import org.example.system.mapper.PostMapper;
import org.example.system.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 帖子服务层
 */
@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;

    /**
     * 按条件分页查询
     * 影响条件有帖子标题
     * @param post
     * @return
     */
    @Override
    public IPage<Post> selectPage(Post post){
        MPJLambdaWrapper<Post> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Post.class);
        wrapper.leftJoin(User.class, User::getId, Post::getUserId);
        if (post.getUserId() != null){
            wrapper.eq(Post::getUserId,post.getUserId());
        }
        if (post.getStatus() != null){
            wrapper.eq(Post::getStatus,post.getStatus());
        }
        wrapper.selectAs(User::getNickname,Post::getNickname);
        if (StrUtil.isNotEmpty(post.getNickname())){
            wrapper.like(Post::getNickname,post.getNickname());
        }
        IPage<Post> page = new Page<>();
        page.setCurrent(post.getPageNum());
        page.setSize(post.getPageSize());
        return postMapper.selectPage(page,wrapper);
    }

    /**
     * 按分页查询联查帖子表,区分
     * @param post
     * @return
     */
    @Override
    public IPage<Post> select(Post post){
        MPJLambdaWrapper<Post> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Post.class);
        wrapper.leftJoin(User.class, User::getId, Post::getUserId);
        if (post.getUserId() != null){
            wrapper.eq(Post::getUserId,post.getUserId());
        }
        if (post.getStatus() != null){
            wrapper.eq(Post::getStatus,post.getStatus());
        }
        wrapper.selectAs(User::getNickname,Post::getNickname);
        wrapper.selectAs(User::getAvatarPath,Post::getAvatarPath);
        wrapper.orderByDesc(Post::getId);
        IPage<Post> page = new Page<>();
        page.setCurrent(post.getPageNum());
        page.setSize(post.getPageSize());
        return postMapper.selectPage(page,wrapper);
    }

    /**
     * 添加帖子
     * @param post
     * @return
     */
    @Override
    public int add(Post post) {
        post.setCreateTime(new Date());
        post.setNote("帖子创建成功！");
        return postMapper.insert(post);
    }

    /**
     * 修改
     * @param post
     * @return
     */
    @Override
    public int update(Post post) {
        post.setUpdateTime(new Date());
        post.setNote("修改帖子数据");
        return postMapper.updateById(post);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return postMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @Override
    public Post getOne(Long id) {
        MPJLambdaWrapper<Post> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Post.class);
        wrapper.leftJoin(User.class, User::getId, Post::getUserId);
        wrapper.eq(Post::getId,id);
        wrapper.selectAs(User::getNickname,Post::getNickname);
        wrapper.selectAs(User::getAvatarPath,Post::getAvatarPath);
        return postMapper.selectOne(wrapper);
    }

    /**
     * 根据用户ID获取用户发布的帖子数量
     * @param userId
     * @return
     */
    @Override
    public Long getPostCount(Long userId) {
        QueryWrapper<Post> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return postMapper.selectCount(wrapper);
    }
}
